package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.*;

import concole.IOHelper;

public class CourseOpPanel extends JPanel {
	JButton searchbt;
	JTextArea courseMessage;
	JTextField searchfd;
	protected Font middlFont = new Font("serif", Font.BOLD, 20);

	public CourseOpPanel() {
		super();
		searchfd = new JTextField();
		searchfd.setBounds(300, 50, 200, 30);
		this.setLayout(null);
		this.setBackground(Color.green);
		this.setBounds(0, 0, 600, 300);
		searchbt = new JButton("SearchCourse");
		searchbt.setFont(middlFont);
		searchbt.setBounds(50, 50, 200, 30);
		searchbt.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String courseInfoString = getCourseInfo(searchfd.getText());
				if (courseInfoString == null||courseInfoString.length()==0) {
					JOptionPane.showMessageDialog(null,
							"sorry course not existence");
					return;

				}
				String toshowString = getShowString(courseInfoString);
				courseMessage.setText("");
				courseMessage.setText(toshowString);
			}
		});

		courseMessage = new JTextArea(3, 3);
		courseMessage.append("The course message:");
		courseMessage.setBounds(50, 100, 460, 120);
		this.add(searchbt);
		this.add(courseMessage);
		this.add(searchfd);

	}

	protected String getCourseInfo(String text) {
		// TODO Auto-generated method stub
		return IOHelper.findOneRecord(0, "courseList.txt", text);
	}

	public String getShowString(String courseInfo) {
		StringBuffer bf = new StringBuffer();
		String[] strings = courseInfo.split(" ");
		final String NAME = "Course Name: ";
		final String NUMBER = "Course Number: ";
		final String PLACE = "Place: ";
		final String CREDIT = "Credit: ";
		final String TIME = "Time: ";
		final String TEACHER = "Teachers: ";
		final String TAB = "\t";
		final String NEWLINE = "\n";
		bf.append("The course message:" + NEWLINE);
		bf.append(NAME);
		bf.append(strings[1]);
		bf.append(TAB);
		bf.append(NUMBER);
		bf.append(strings[0]);
		bf.append(NEWLINE);
		bf.append(TEACHER);
		bf.append(strings[2]);
		bf.append(NEWLINE);
		bf.append(PLACE);
		bf.append(strings[10]);
		bf.append(NEWLINE);
		bf.append(CREDIT);
		bf.append(strings[6]);
		bf.append(NEWLINE);
		bf.append(TIME);
		bf.append(strings[9]);
		bf.append(NEWLINE);

		return bf.toString();
	}

	public String getCourseNum() {
		return searchfd.getText();
	}
}